package com.atguigu.controller;

import com.atguigu.pojo.Employee;
import com.atguigu.service.EmployeeService;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
@Slf4j
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;

    @RequestMapping("/employee/findAll")
    public String findAll(Model model){
        List<Employee> empList = this.employeeService.findAll();
        model.addAttribute("empList",empList);
        return "empList";
    }
    @RequestMapping("/employee/findByPage")
    public String findByPage(@RequestParam(required = false,defaultValue = "1") Integer pageNum,Model model){
        Integer pageSize = 5;
        PageInfo  pageInfo = this.employeeService.findByPage(pageNum,pageSize); //pageInfo中包括分页的所有内容

        model.addAttribute("pageInfo",pageInfo);
        return "empList";

    }

    @RequestMapping("/employee/saveEmp")
    public String saveEmp(Employee emp){
        this.employeeService.saveEmp(emp);
        return "redirect:/employee/findAll";
    }
}
